<?php
$todayBegin = strtotime(date('Y-m-d',time()).' 00:00:00');
$todayEnd = $todayBegin + 60 * 60 * 24;
//初始化10天数据
$theseDayBill = array();
for($i=9; $i>=0; $i--) {
    $theseDayBill[$i] = 0;
}
//获取交易的数据
$BAWeBillCooker = new BAWeBillCooker();
$billType = $BAWeBillCooker->getCurrentBillType();
//$billTypeId = $billType->getId();
$billView = $BAWeBillCooker->getBillList($billType);
//1、通过$billTime提取最近10天交易数据，并赋值给$theseDayBill
//2、赋值给图表
$billTime = '';
$payAmount = '';
foreach ($billView as $billList) {
    if ($billList instanceof BTreeList) {
        if($billList->getItem('billTime') != NULL){
            $billTime = $billList->getItem('billTime')->value;
        }
        if($billList->getItem('payAmount') != NULL){
            $payAmount = $billList->getItem('payAmount')->value;
        }
        switch ($billTime) {
            case ($billTime>=$todayBegin && $billTime<=$todayEnd):
                $theseDayBill[0] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*1 && $billTime<=$todayEnd-60*60*24*1):
                $theseDayBill[1] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*2 && $billTime<=$todayEnd-60*60*24*2):
                $theseDayBill[2] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*3 && $billTime<=$todayEnd-60*60*24*3):
                $theseDayBill[3] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*4 && $billTime<=$todayEnd-60*60*24*4):
                $theseDayBill[4] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*5 && $billTime<=$todayEnd-60*60*24*5):
                $theseDayBill[5] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*6 && $billTime<=$todayEnd-60*60*24*6):
                $theseDayBill[6] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*7 && $billTime<=$todayEnd-60*60*24*7):
                $theseDayBill[7] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*8 && $billTime<=$todayEnd-60*60*24*8):
                $theseDayBill[8] += $payAmount;
                break;
            case ($billTime>=$todayBegin-60*60*24*9 && $billTime<=$todayEnd-60*60*24*9):
                $theseDayBill[9] += $payAmount;
                break;
            default :
                break;
        }
    }
}
?>

<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl.'/js/mobile/Chart.min.js'?>"></script>
<p>消费总金额趋势图 </p>
<p style="margin-top: 60px;float: left;width: 20px;">金额</p>
<canvas id="chart-line" width="300" height="200"></canvas>
<p style="margin-left: 150px;">日期 </p>
 <script>
        var data = {
            labels: [<?php 
                    foreach ($theseDayBill as $key => $value) {
                        if($key == 0) {?>
                            "<?php echo '今天';?>"<?php
                        } else {?>
                            "<?php echo date('m-d',$todayBegin-60*60*24*$key);?>",<?php
                        }
                    }?>],
            datasets: [
                {
                    label: "My Second dataset",
                    fillColor: "rgba(151,187,205,0.2)",
                    strokeColor: "rgba(151,187,205,1)",
                    pointColor: "rgba(151,187,205,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(151,187,205,1)",
                    data: [<?php 
                            foreach ($theseDayBill as $key => $value) {
                                if($key == 0) {
                                    echo $value;
                                } else {
                                    echo $value.',';
                                }
                            }?>]
                }
            ]
        };
        function addChart(){

            var ctx1 = document.getElementById("chart-line").getContext("2d");
            var myLineChart = new Chart(ctx1).Line(data, {
                segmentShowStroke : true
            });
        };
        addChart();
 </script>
